#pragma once

#include "net_common.h"
#include "network_message/network_message.h"
#include "network_message/network_message_handler.h"

class network_message_handler;

class session :
    public std::enable_shared_from_this<session>,
    private boost::noncopyable {
public:
    session(tcp::socket socket) : socket_(std::move(socket))
    {

    }

    ~session()
    {
        socket_.close();
    }

    int start()
    {
        read_header();
        return NET_EC_OK;
    }

    void do_read()
    {
        read_header();
    }

    int send_message(const std::string &msg);
private:
    void on_read_header_complete(boost::system::error_code ec, std::size_t length);
    int read_header();
    void on_read_body_complete(boost::system::error_code ec, std::size_t length);
    int read_body(uint16_t size_to_read);
    int current_body_size() const
    {
        return cur_msg_header_.msg_size_;
    }

    uint32_t heaer_beat_seq_ = 0;   /* 心跳保活机制序列号 */
    uint16_t readed_header_size_ = 0;
    uint16_t readed_body_size_ = 0;
    message_header cur_msg_header_ = { 0 };
    char msg_header_buf_[NET_MESSAGE_HEADER_SIZE] = { 0 };  /* msg_header_buf_用来缓存读取的消息头，\
        当读取到一个完整的消息头时，将消息头拷贝到cur_msg_header_中，msg_header_buf_继续用于读取下一条消息头 */
    char msg_body_buf_[NET_MESSAGE_BODY_MAX_SIZE] = { 0 };
    tcp::socket socket_;
};
